package com.suntop.carousel.qlmediaplay.banner;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;

import com.suntop.carousel.qlmediaplay.adapter.FragmentBrandAdapter;
import com.suntop.carousel.qlmediaplay.bean.Option;
import com.suntop.carousel.qlmediaplay.bean.PlayerState;
import com.suntop.carousel.qlmediaplay.field.Field_Banner_Player;
import com.suntop.carousel.qlmediaplay.media.IMediaPlayEventListener;
import com.suntop.carousel.qlmediaplay.tools.PlayerUtils;
import com.suntop.carousel.qlmediaplay.tools.Rxbus;

import java.util.ArrayList;

import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentViewHolder;
import io.reactivex.functions.Consumer;

public class NewFragmentBanner extends NewViewPagerBanner<Option, FragmentBrandAdapter,
        FragmentViewHolder> implements IMediaPlayEventListener , Consumer<PlayerState> {

    public NewFragmentBanner(Context context) {
        this(context, null);
    }

    public NewFragmentBanner(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NewFragmentBanner(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void callBackPageScrolled(int realPosition) {

    }


    @Override
    protected FragmentBrandAdapter getAdapterForViewPager() {
        Context context = PlayerUtils.scanForActivity(getContext());
        if (context == null) {
            return null;
        }
        if (context instanceof FragmentActivity)
            return new FragmentBrandAdapter(((FragmentActivity) context).getSupportFragmentManager()
                    , ((FragmentActivity) context).getLifecycle()).setRestTo(false);
        else
            return null;
    }

    @Override
    public void addResource(ArrayList<Option> arrayList) {
        adapter.setIMediaPlayEventListener(this);
        adapter.setSources(arrayList);
//        if (arrayList.size()>1){
//            setOffscreenPageLimit(adapter.getItemCount());
//        }

    }


    @Override
    protected void register(String tag) {

        Rxbus.getInStance()
                .registerRxbus(Field_Banner_Player.Fragment_Banner_Player,
                        tag, this);
    }

    @Override
    protected void unRegister(String tag) {
        Rxbus.getInStance().unRegisterRxBus(Field_Banner_Player.Fragment_Banner_Player,
                tag);
    }

    @Override
    public void startMediaPlay(int position, int type, String url) {
        Log.e("startMediaPlay", "开始播放" + position);
    }

    @Override
    public void cacheMediaPlay(int position, int type, String url, String path) {

        Log.e("cacheMediaPlay", "cacheMediaPlay" + position);
    }

    @Override
    public void finishMediaPlay(int position, int type, String url, String error) {
        if (adapter == null) {
            return;
        }
        Log.e("finishMediaPlay", "finishMediaPlay" + position);
        playNext();
    }

    @Override
    public void accept(PlayerState playerState) throws Exception {
        super.accept(playerState);
    }
}
